package com.example.shop.controller;

import com.example.shop.domain.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.UUID;


/**
 * @projectName: travel
 * @package: com.example.travel.controller
 * @className: FileController
 * @author: Liqian
 * @description: TODO
 * @date: 2024/4/19 13:37
 * @version: 1.0
 */

@Tag(name = "文件管理", description = "这是描述")
@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {
    @Value("${takeaway.path}")
    private String basePath;
    @Value("${iocn.path}")
    private String iconPath;
    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    public Result<String> upload(MultipartFile file) throws IOException {
        //创建一个目录对象
        File dir = new File(basePath);
        //判断当前目录是否存在
        if(!dir.exists()){
            //目录不存在，需要创建
            dir.mkdirs();
        }
        //存储文件
        String originalFilename = file.getOriginalFilename();
        //保证文件名唯一性
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));


        file.transferTo(new File(basePath + filename));
        return Result.success(filename);

    }

    /**
     * 文件下载
     * @param name
     * @param response
     */

    @Operation(summary = "获取图片")
    @GetMapping("/img")
    public void download(String name, HttpServletResponse response){

        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fileInputStream =
                    new FileInputStream(new File(basePath + name));
            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();
            response.setContentType("image/jpeg");
            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }
            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    /**
     * 文件下载
     * @param name
     * @param response
     */
    @GetMapping("/icon")
    @Operation(summary = "获取图标")
    public void icon(String name, HttpServletResponse response){

        try {
            //输入流，通过输入流读取文件内容
            FileInputStream fileInputStream = new FileInputStream(new File(iconPath+"\\category\\" + name));

            //输出流，通过输出流将文件写回浏览器
            ServletOutputStream outputStream = response.getOutputStream();

            response.setContentType("image/jpeg");

            int len = 0;
            byte[] bytes = new byte[1024];
            while ((len = fileInputStream.read(bytes)) != -1){
                outputStream.write(bytes,0,len);
                outputStream.flush();
            }

            //关闭资源
            outputStream.close();
            fileInputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }


}
